<?php

namespace App\Http\Controllers\Me;

use Illuminate\Http\Request;

use App\Demo;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class demoController extends Controller
{
    /**
     * 博客首页
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $demo = Demo::get();
        return view('me.index',['demo'=>$demo]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('me.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //文章添加
        $date =new Demo();
        $date -> title =$request->title;
        $date -> content = $request->content;
        if(empty($date)){
            return back()->with('info','标题和内容不能为空');
        }
        if($date->save()){
            return redirect('/demo')->with('info','添加成功');
        }else{
            return back()->with('info','添加失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
        $demo = Demo::where('id',$id)->get()[0];
        // dd($demo);die;
        return view('me.show',['demo'=>$demo]);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $demo = Demo::where('id',$id)->get()[0];

        return view('me.edit',['demo'=>$demo]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
        $date = Demo::findOrFail($id);
        $date -> title =$request->title;
        $date -> content = $request->content;
        if(empty($date)){
            return back()->with('info','标题和内容不能为空');
        }
        if($date->update()){
            return redirect('/demo')->with('info','修改成功');
        }else{
            return back()->with('info','修改失败');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
        $demo= Demo::find($id);
        if($demo->delete()){
            return redirect('/demo');
        }else{
            return back();
        }
    }
}
